<?php

namespace app\api\model;

use app\admin\model\MemberModel;
use think\Db;
use think\Model;


class WithdrawalModel extends ApiModel
{


    protected $status = [0=>'申请中',1=>'已打款',2=>'拒绝提现'];
    // 表名
    protected $name = 'withdrawal';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    public function withdrawal($params)
    {
        $member_id = $params['uid'] ?? 0;
        $money = $params['money'] ?? 0;
        $type = $params['type'] ?? 1;
        $member = (new \app\api\model\MemberModel())->where(['id' => $member_id])->find();
        if ($money < 0) {
            return $this->api_error('提现金额不正确');
        }
        if($money > 500){
            return $this->api_error('提现金额最多500元');
        }
        try {
            Db::startTrans();
            if ($type == 1) {
                //团队金额提现
                if ($member['team_total_money'] < $money) {
                    return $this->api_error('提现金额不正确');
                }
                //减金额
                (new MemberModel())->where(['id' => $member_id])->setDec('team_total_money', $money);
                $title = "团队佣金提现" . $money . "元";
                $before_money = $member['team_total_money'];

            } elseif ($type == 2){
                //财税顾问金额提现
                if ($member['advise_total_money'] < $money) {
                    return $this->api_error('提现金额不正确');
                }
                (new MemberModel())->where(['id' => $member_id])->setDec('advise_total_money', $money);
                $title = "财税顾问提现" . $money . "元";
                $before_money = $member['advise_total_money'];
            }else{
                //保证金
                if ($member['bond'] < $money) {
                    return $this->api_error('提现金额不正确');
                }
                (new MemberModel())->where(['id' => $member_id])->setDec('bond', $money);
                $title = "财税顾问提现" . $money . "元";
                $before_money = $member['bond'];
            }

            //生成提现记录
            $data['type'] = $type;
            $data['out_trade_no'] = (new Pay())->build_order_no('WD');
            $data['member_id'] = $member_id;
            $data['title'] = $title;
            $data['before_money'] = $before_money;
            $data['money'] = $money;
            $data['create_time'] = time();
            self::insert($data);
            Db::commit();
            return $this->api_success('申请成功');

        } catch (\Exception $e) {
           Db::rollback();
            return $this->api_success('操作失败');
        }

    }


    //提现记录
    public function userWithdrawal($params = array()){
        $type = $params['type'] ?? 1;
        $list = self::where(['member_id' => $params['uid'],'type' => $type])
            ->field('id,title,money,create_time,status')
            ->paginate(10);

       foreach ($list as &$value){
           $value['status'] = $this->status[$value['status']];
       }

       return $this->api_success('获取成功',$list);
    }

}
